package com.example.day35.service.impl;

import com.example.day35.dao.IUserDao;
import com.example.day35.model.PageInfos;
import com.example.day35.model.User;
import com.example.day35.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class UserServiceImpl implements IUserService {
    private final IUserDao userDao;
    @Override
    public List<User> list() {
        return userDao.list();
    }

    @Override
    public int add(User user) {
        return userDao.add(user);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int delete(int id) {
        return userDao.delete(id);
    }

    @Override
    public int batchDelete(List<Integer> ids) {
        return userDao.batchDelete(ids);
    }


    @Override
    public PageInfos<User> findAllUsersByPage(Integer pageNum, Integer pageSize) {
        List<User> users = userDao.list();

        PageInfos<User> pageInfos = new PageInfos<User>(pageNum, pageSize);

        //判断是否是最后一页,如果最后一页就显示最后一页的几条，不是最后一页就显示pageSize条
        if(pageNum== pageInfos.getTotal())
            pageInfos.setList(users.subList((pageNum-1)*pageSize, users.size()));
        else
            pageInfos.setList(users.subList((pageNum-1)*pageSize, (pageNum-1)*pageSize+pageSize));
        return pageInfos;
    }



}
